/*
 * 文 件 名:  CodeTableTag.java
 * 版    权:  Ysten Technologies Co., Ltd. Copyright YYYY-YYYY,  All rights reserved
 * 描    述:  <描述>
 * 修 改 人:  bo.chen
 * 修改时间:  2014年12月11日
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */
package lv.pig.base.jsp;

import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.jsp.JspWriter;

import lv.pig.base.utils.SpringUtils;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.tags.RequestContextAwareTag;

/**
 * <codetable自定义标签> <功能详细描述>
 * 
 * @author bo.chen
 * @version [版本号, 2014年12月11日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Component
public class CodeTableTag extends RequestContextAwareTag
{
    private static final long serialVersionUID = -5570316715374192543L;
    
    private String tableName;
    
    private String dataCol;
    
    private String valCol;
    
    private String whereClause;
    
    private String value;
    
    private static final Logger logger = LoggerFactory.getLogger(CodeTableTag.class);
    
    /**
     * @return 返回 tableName
     */
    public String getTableName()
    {
        return tableName;
    }
    
    /**
     * @param 对tableName进行赋值
     */
    public void setTableName(String tableName)
    {
        this.tableName = tableName;
    }
    
    /**
     * @return 返回 dataCol
     */
    public String getDataCol()
    {
        return dataCol;
    }
    
    /**
     * @param 对dataCol进行赋值
     */
    public void setDataCol(String dataCol)
    {
        this.dataCol = dataCol;
    }
    
    /**
     * @return 返回 valCol
     */
    public String getValCol()
    {
        return valCol;
    }
    
    /**
     * @param 对valCol进行赋值
     */
    public void setValCol(String valCol)
    {
        this.valCol = valCol;
    }
    
    /**
     * @return 返回 whereClause
     */
    public String getWhereClause()
    {
        return whereClause;
    }
    
    /**
     * @param 对whereClause进行赋值
     */
    public void setWhereClause(String whereClause)
    {
        this.whereClause = whereClause;
    }
    
    /**
     * @return 返回 value
     */
    public String getValue()
    {
        return value;
    }
    
    /**
     * @param 对value进行赋值
     */
    public void setValue(String value)
    {
        this.value = value;
    }
    
    private StringBuffer buildSql()
    {
        StringBuffer sb = new StringBuffer();
        sb.append("select " + this.dataCol + " from ");
        sb.append(this.tableName);
        sb.append(" where 1=1 ");
        if (!StringUtils.isEmpty(this.value))
        {
            sb.append(" and " + this.valCol + " =?");
        }
        if (!StringUtils.isEmpty(this.whereClause))
        {
            sb.append(" and " + this.whereClause);
        }
        
        return sb;
    }
    
    protected Object getCodeTableValue()
    {
        StringBuffer sb = this.buildSql();
        JdbcTemplate jdbcTemplate = SpringUtils.getBean(JdbcTemplate.class);
        return jdbcTemplate.query(sb.toString(), new Object[] {}, new ResultSetExtractor<Object>()
        {
            
            @Override
            public Object extractData(ResultSet rs)
                throws SQLException, DataAccessException
            {
                return rs.getObject(valCol);
            }
            
        });
    }
    
    public String toString()
    {
        return this.tableName + this.dataCol + this.valCol + this.value + this.whereClause;
    }
    
    @Override
    protected int doStartTagInternal()
        throws Exception
    {
        
        try
        {
            JspWriter out = this.pageContext.getOut();
            Object value = getCodeTableValue();
            out.println((null == value) ? "" : value);
            
        }
        catch (Exception e)
        {
            logger.info("生成标签失败!查无数据或者异常" + e.getMessage());
        }
        return SKIP_BODY;
    }
}
